package com.itheima.stock.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.itheima.stock.cache.Cache;
import com.itheima.stock.constant.Constant;
import com.itheima.stock.dto.LoginDTO;
import com.itheima.stock.entity.SysUser;
import com.itheima.stock.enums.CodeEnum;
import com.itheima.stock.exception.Exceptions;
import com.itheima.stock.mapper.SysUserMapper;
import com.itheima.stock.service.UserService;
import com.itheima.stock.util.IdWorkers;
import com.itheima.stock.util.RedisKeyUtil;
import com.itheima.stock.vo.CaptchaVO;
import com.itheima.stock.vo.LoginVO;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

/**
 * @author zxh
 * @created 2024/11/1 14:07
 * @description
 */
@Service
public class UserServiceImpl implements UserService {

    private final Cache cache;

    private final IdWorkers idWorkers;
    private final PasswordEncoder passwordEncoder;
    /**
     * 验证码长度
     */
    private static final int CAPTCHA_LENGTH = 4;
    private final SysUserMapper sysUserMapper;

    public UserServiceImpl(Cache cache, IdWorkers idWorkers, SysUserMapper sysUserMapper, PasswordEncoder passwordEncoder) {
        this.cache = cache;
        this.idWorkers = idWorkers;
        this.sysUserMapper = sysUserMapper;
        this.passwordEncoder = passwordEncoder;
    }

    @Override
    public CaptchaVO captcha() {
        // 1.随机生成4位验证码
        String captcha = RandomStringUtils.randomNumeric(CAPTCHA_LENGTH);
        // 2.缓存验证码到redis中
        // 2.1 生成redis 的key
        String redisKey = RedisKeyUtil.generateKey(Constant.CAPTCHA_PREFIX, idWorkers.nextStrId());
        // 2.2 调用缓存接口存入验证码
        cache.set(redisKey, captcha, Constant.CAPTCHA_EXPIRE_TIME);
        // 3. 数据组装返回
        return CaptchaVO.builder()
                .code(captcha)
                .rkey(redisKey)
                .build();
    }

    @Override
    public LoginVO login(LoginDTO loginDTO) {
        //参数校验
        if(ObjectUtils.isEmpty(loginDTO) 
                || ObjectUtils.isEmpty(loginDTO.getUsername())
                || ObjectUtils.isEmpty(loginDTO.getPassword())
                || ObjectUtils.isEmpty(loginDTO.getCode())
                || ObjectUtils.isEmpty(loginDTO.getRkey())){
            Exceptions.cast(CodeEnum.PARAM_NOT_EMPTY);
        }
        String captcha = cache.get(loginDTO.getRkey());
        if(!StringUtils.equals(captcha,loginDTO.getCode())){
            Exceptions.cast(CodeEnum.SYSTEM_VERIFY_CODE_ERROR);
        }
        cache.remove(loginDTO.getRkey());
        SysUser sysUser =sysUserMapper.selectByUsername(loginDTO.getUsername(),Constant.SYS_USER_STATUS_NORMAL,Constant.SYS_USER_UNDELETE);
        if(ObjectUtils.isEmpty(sysUser) || !passwordEncoder.matches(loginDTO.getPassword(),sysUser.getPassword())){
            Exceptions.cast(CodeEnum.SYSTEM_PASSWORD_ERROR);
        }
        return LoginVO.builder()
                .id(sysUser.getId())
                .username(sysUser.getUsername())
                .nickName(sysUser.getNickName())
                .phone(sysUser.getPhone())
                .build();




    }


}
